package com.example.fx.detail

import android.R.attr.thumb
import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.app.ActionBar.LayoutParams
import android.content.Context
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.example.base.mvvm.BaseActivity
import com.example.fx.R
import com.example.fx.adapter.CommentAdapter
import com.example.fx.databinding.ActivityDetailBinding
import com.example.fx.utils.GlideUtils
import com.google.zxing.BarcodeFormat
import com.journeyapps.barcodescanner.BarcodeEncoder
import com.umeng.socialize.ShareAction
import com.umeng.socialize.media.UMWeb
import com.youth.banner.loader.ImageLoader


@Suppress("DEPRECATION")
@Route(path = "/app/DetailActivity")
class DetailActivity : BaseActivity<DetailViewModel,ActivityDetailBinding>() {
    val imgs = mutableListOf<String>()//存放轮播图图片
    @Autowired
    @JvmField
    var goodsId = 0

    var attrId = ""//属性id
    var imgurl = ""//图片网址，方便后续加入购物车显示图片的动画

    var index = 1;//记录现在点击的下标

    lateinit var commentAdapter: CommentAdapter
    override fun getLayout(): Int  = R.layout.activity_detail

    override fun getViewModel(): Class<DetailViewModel>  = DetailViewModel::class.java

    override fun initView() {
        ARouter.getInstance().inject(this)
        viewModel.detail(goodsId,"normal")

        commentAdapter = CommentAdapter()
        binding.rvCommet.adapter = commentAdapter
        binding.rvCommet.layoutManager =LinearLayoutManager(this)




        //点击加入购物车
        binding.addCar.setOnClickListener {
            viewModel.addCar(1,attrId,goodsId)
        }
        //点击返回
        binding.ivBack.setOnClickListener {
            finish()
        }
        //点击分享
        binding.ivShare.setOnClickListener {
            val web: UMWeb = UMWeb(imgurl)
           /* web.title = "This is music title" //标题

            web.setThumb(thumb) //缩略图

            web.description = "my description" //描述*/

            ShareAction(this)
                .withMedia(web)
                .open()
        }
        //点击跳转到购物车页面
        binding.toCar.setOnClickListener {
            //ARouter.getInstance().build("/app/MainActivity").withInt("index",2).navigation()
            //验证评论是否好使
            viewModel.commentGet()
            //验证发送评论是否好使
            //viewModel.comment("测试这么写是否可行",163)
        }
        //点击item,实现加载二级评论
        commentAdapter.setOnItemClickListener { adapter, view, position ->
            index = position
            viewModel.commentChildGet(commentAdapter.data[position].rootid)

        }
    }

    override fun initData() {
        viewModel.detailSuccess.observe(this){
            if(it.code==200){
                binding.info = it.data.productInfo
                attrId = it.data.productAttr[0].id.toString()//获取属性id，方便后续加入购物车
                imgurl = it.data.productInfo.image//动画效果的图片
                //显示轮播图
                imgs.add(it.data.productInfo.image)
                imgs.add("http://10.161.9.80:8080/crmebimage/public/maintain/2021/12/25/ae020f17148b4cc19bcab221c0836dd1iy0vrck2jm.png")
                binding.banner.setImages(imgs)
                    .setImageLoader(object :ImageLoader(){
                        override fun displayImage(
                            context: Context?,
                            path: Any?,
                            imageView: ImageView?
                        ) {
                            if (context != null&&imageView!=null) {
                                Glide.with(context).load(path).into(imageView)
                            }
                        }
                    })
                    .start()
                //显示二维码
                val barcodeEncoder = BarcodeEncoder()
                //BarcodeFormat.CODE_128条形码 不能有中文  QR_CODE 二维码
                val bitmap = barcodeEncoder.encodeBitmap(
                    it.data.productInfo.storeName,
                    BarcodeFormat.QR_CODE,
                    300,
                    300
                )
                binding.code.setImageBitmap(bitmap)
            }else{
                ToastUtils.showLong(it.message)
            }
        }
        viewModel.addCarSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                //添加购物车成功，展示动画效果
                var imageview = ImageView(this)
                val params = LayoutParams(200, 200)
                imageview.layoutParams = params//设置图片大小
                GlideUtils.loadImage(this,imgurl,imageview)//显示图片
                binding.clDetail.addView(imageview)//显示在页面上
                //设置动画效果
                val width = windowManager.defaultDisplay.width.toFloat()//屏幕宽度
                val height = windowManager.defaultDisplay.height.toFloat()//屏幕高度
                val tranX = ObjectAnimator.ofFloat(imageview, "translationX", 0f, width)//水平移动
                val tranY = ObjectAnimator.ofFloat(imageview, "translationY", 0f, height)//垂直一定
                val animatorSet = AnimatorSet()
                animatorSet.duration = 2000
                animatorSet.play(tranX).with(tranY)
                animatorSet.start()//启动动画
                //当动画效果结束后，图片消失
                animatorSet.addListener(object:Animator.AnimatorListener{
                    override fun onAnimationStart(animation: Animator) {
                    }

                    override fun onAnimationEnd(animation: Animator) {
                        //动画结束
                        binding.clDetail.removeView(imageview)
                    }

                    override fun onAnimationCancel(animation: Animator) {
                    }

                    override fun onAnimationRepeat(animation: Animator) {
                    }
                })

            }
        }
        viewModel.getSuccess.observe(this){
            ToastUtils.showLong(it.data[0].toString())
            commentAdapter.data.clear()
            commentAdapter.data.addAll(it.data)
            commentAdapter.notifyDataSetChanged()
        }
        viewModel.commentSuccess.observe(this){
            ToastUtils.showLong(it.message)

        }
        //显示出来
        viewModel.childGetSuccess.observe(this){
            if(it.code==200){
                commentAdapter.data.addAll(index,it.data)
                commentAdapter.notifyDataSetChanged()
            }
        }
    }


}